package cn.shenlong.front.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.shenlong.common.Pagination;
import cn.shenlong.portal.domain.Company;
import cn.shenlong.portal.domain.Icon;
import cn.shenlong.portal.service.CompanyService;
import cn.shenlong.portal.service.IconService;
import cn.shenlong.util.StrUtils;

@Controller
public class Directories {
	private static final Logger log = LoggerFactory.getLogger(Directories.class);
	
	@Autowired
	private CompanyService companyService;
	@Autowired
	private IconService iconService;
	
	@RequestMapping(value = "/front/directories", method = RequestMethod.GET)
	public String index(Integer pageNumber, Integer pageSize,HttpServletRequest request, ModelMap model) {		
		if (pageNumber == null) {
			pageNumber = 1;
		}
		if (pageSize == null) {
			pageSize = 4;
		}
		
		Company company = new Company();
		Pagination p  = companyService.findCompany(company, null, pageSize, pageNumber);
		
		List<Company> lst = (List<Company>) p.getList();
		for (Company c : lst) {
			if (c != null && c.getIconId() != null) {
				Icon icon = this.iconService.findIconByIconId(c.getIconId());
				c.setIcon(icon);
  
			}
			if(c!=null && c.getDescription()!=null){
				c.setDescription(StrUtils.htmlToText(c.getDescription()));
			}
		}
		
		Integer lastPage = p.getTotalCount() % pageSize == 0 ? p.getTotalCount() / pageSize : p.getTotalCount() / pageSize + 1;
		Integer prePage = pageNumber <= 1 ? 1 : pageNumber - 1;
		Integer nextPage = pageNumber >= lastPage ? lastPage : pageNumber + 1;
		model.put("page", p);
		model.put("lastPage", lastPage);
		model.put("prePage", prePage);
		model.put("nextPage", nextPage);
		model.put("pageNumber", pageNumber);

		List<Integer> lstPage = new ArrayList<Integer>();
		for (int i = pageNumber <= 3 ? 1 : pageNumber - 3; i <= pageNumber + 4 && i <= lastPage; i++) {
			lstPage.add(i);
		}
		model.put("lstPage", lstPage);
		return "/front/directories";

	}	
	
	@RequestMapping(value = "/front/directoryshow", method = RequestMethod.GET)
	public String directoriesshow(Integer id,HttpServletRequest request, ModelMap model) {
		Company company = companyService.findCompanyByCompanyId(id);
		if (company != null && company.getIconId() != null) {
			Icon icon = this.iconService.findIconByIconId(company.getIconId());
			company.setIcon(icon);
		}
		model.put("company", company);
		return "/front/directoryshow";

	}
	
	@RequestMapping(value = "/front/directorypreview", method = RequestMethod.POST)
	public String directoriespreview(Integer id,HttpServletRequest request, ModelMap model) {
		Company company = new Company();//companyService.findCompanyByCompanyId(id);
		company.setCompanyName(request.getParameter("companyName"));
		company.setAddress(request.getParameter("address"));
		company.setLinkMan(request.getParameter("linkMan"));
		company.setTelephone(request.getParameter("telephone"));
		company.setEmail(request.getParameter("email"));
		company.setDescription(request.getParameter("description"));
		company.setImgUrl(request.getParameter("imgUrl"));		
		String iconId = request.getParameter("iconId");
		
		if (iconId != null && iconId.trim().length() > 0) {
			Icon icon = this.iconService.findIconByIconId(Long.parseLong(iconId));
			company.setIcon(icon);
		} 
		model.put("company", company);
		return "/front/directorypreview";

	}
}
